/**
 * 中医查体的视力，眼底动脉，颈动脉，心，肝，肺，浮肿，等的隐藏、显示
 */
$(function(){
	var gridData = [{orgName: '爱民诊所', clinicDate: '2016-03-15',drName:'王强'},
   	 /*病历数据*/     {orgName: '爱民诊所', clinicDate: '2016-03-15',drName:'王强'},
   	         	    {orgName: '爱民诊所', clinicDate: '2016-03-15',drName:'王强'}];
   	//患者的就诊日期 展示datagrid 
   	$('#pcaseViewGrid').datagrid({   
   	    singleSelect:true,
   	    rownumbers:true,
   	    columns:[[    
   	        {field:'clinicDate',title:'就诊日期',width:'33%',halign:'center'}, 
   	        {field:'orgName',title:'中医馆',width:'30%',halign:'center'},  
   	        {field:'drName',title:'就诊医生',width:'32%',halign:'center'}    
   	    ]] ,
   	    data: gridData,
   		sortable:true,
   		onClickCell: function(index,field,value){
//   			showpcaseInfo();
   		}
   	});
   	
   	$("#templateDataGrid").datagrid({   
   	    singleSelect:true,
   	    rownumbers:true,
   	    columns:[[    
   	        {field:'clinicDate',title:'就诊日期',width:'33%',halign:'center'}, 
   	        {field:'orgName',title:'中医馆',width:'30%',halign:'center'},  
   	        {field:'drName',title:'就诊医生',width:'32%',halign:'center'}    
   	    ]] ,
   	    data: gridData,
   		sortable:true,
   		onClickCell: function(index,field,value){
   			showtemplateInfo();
   		}
   	});
	
	var data={name:"张三",gender:"男",bloodType:"O",birthdday:"2010-01-01"
		,contactPhone:"1277777766",identifyCardID:"",nowAddress:"北京"};
	$('#patientCaseForm').form('load', data);
	//中医查体，心肝肺等信息
	$('.inspect-list label').bind("click", function(event){
		$('#'+this.htmlFor).toggle();//显示或隐藏
		$('#'+this.htmlFor+'text').val(''); //每次隐藏清空数据
		$('#'+this.htmlFor+'text').focus();//获取焦点位置到textarea
	});
/**
 * 中医诊断的datagrid数据表格配置
*/
var editIndex = '';
var data=[{'type':'1','name1':'2','description':'3'}];
var type = [{ "value": "1", "text": "主要诊断" }, { "value": "2", "text": "次要诊断" }];
var name1 =[{"value":"1","text":"头疼"},{"value":"2","text":"感冒"},{"value":"3","text":"发烧"}];
$('#pcaseWriteGrid').datagrid({   //患者的就诊时间 展示datagrid 
//	data:data,
	rownumbers:true,
	columns:[[ 
	     {field: 'ck',title: '',checkbox:'true'},
	     {field:'type',title:'诊断类型',width:'30%',
	    	 editor: { type: 'combobox', options: { 
	    		 data:type, valueField: "value", textField: "text",required:true
	    		 ,editable:false,panelHeight:'auto' }
	    	 },formatter:function(value,row){
				return row.text;
			 }
    	 },
	     {field:'name',title:'诊断名称',width:'30%',
	    	editor: { type: 'combobox', options: { 
	    		data:name1,valueField:"value",textField:"text",required: true
	    		,panelHeight:'auto', editable:true }
	    	 },formatter:function(value,row){
    			return row.text;
    		 }
    	 },
	     {field:'description',title:'描述',width:'30%',
	    		 editor: { type: 'text', options: { required: true }}
    	 }
	 ]] , 
	 onClickRow:function(rowIndex,rowData){
		if (editIndex != rowIndex){
			$('#pcaseWriteGrid').datagrid('endEdit', editIndex);
			$('#pcaseWriteGrid').datagrid('beginEdit', rowIndex);
		}else{
			$('#pcaseWriteGrid').datagrid('beginEdit', rowIndex);
		}
		editIndex = rowIndex;
	}
});
 /**
  * 设置病历模板窗口的大小
  */
$('#add_templateWin').window({    
    width:800,    
    height:600,    
   // modal:true   
});
 /**
  * 病历模板窗口中左侧分栏内 模板名称对应的datagrid表格
  */
$('#templateDataGrid').datagrid({    
	columns:[[    
	    {field:'templateName',title:'模板名称',width:150,editor: { type: 'text', options: { required: true }}},    
	]],
	data:[{name:'模板一'},{name:'模板二'}]
});

});
//保存病历
function fnSavePatientCase(){
	var json = {}, form = $('#patientCaseForm').serializeArray();
	for(var i=0;i<form.length;i++) {
		if(!json[form[i].name]){
			json[form[i].name]=form[i].value;
		}
	}
	//中医查体
	json.pulseInfo="";
	$($("input[name='pulseInfo']:checked")).each(
		function(){
			json.pulseInfo+=this.value +',';
		}
	);
	json.shezhi="";
	$($("input[name='shezhi']:checked")).each(
		function(){
			json.pulseInfo+=this.value +',';
		}
	);
	json.tongue="";
	$($("input[name='tongue']:checked")).each(
		function(){
			json.pulseInfo+=this.value +',';
		}
	);
	$.ajax({
        url:'patient/create',
        type: 'post',
        data: json,
        success: function(data){
        	if(data.success == true){
        		$.messager.show({
        			title:'提示',
        			msg:'保存成功！'
        		});
	            $('#pcaseWriteGrid').datagrid('reload');    
        	}
        }
    });
}
/**
 * 中医诊断的表格，点击添加诊断按钮，自动添加一行
 */
function addClinic_btn(){
	$('#pcaseWriteGrid').datagrid('appendRow',{
		ck:'',
		type: '',
		name: '',
		description: ''
	});
}
/**
 * 中医诊断的表格，点击删除诊断按钮，可进行批量删除
 */
function deleteClinic_btn(){
    var rows = $('#pcaseWriteGrid').datagrid("getSelections"); 
    var selections = [];
    for (var j= 0; j < rows.length; j++){
    	selections.push(rows[j]); 
    }
    if(selections.length == 0){
		$.messager.alert('提示','请选择需要删除的记录！'); 
	}else if(selections.length > 1){
		$.messager.confirm("提示", "确定要删除吗？", function (c) {
	        if (c) {
	        	$('#pcaseWriteGrid').datagrid('unselectAll');
	        	for(var i =0;i<selections.length;i++){    
        	       var index = $('#pcaseWriteGrid').datagrid('getRowIndex',selections[i]);
        	       $('#pcaseWriteGrid').datagrid('deleteRow',index); 
        	    }
	        }
	    });
	}
}

/**
 * 添加病历模板窗口
 */
function fnAddTemplate(){
	$('#add_templateWin').window('open');
}